########################################## I-ON AUTO LOGIN SCRIPT v 3.0 #############################################################
##########################################        MADE BY DJAY          #############################################################
##########################################  http://djkaos.wordpress.com #############################################################


################### MODIFY THIS SCRIPT AT YOUR OWN RISK ##################################################





from getpass import getpass
from urllib2 import Request,urlopen
from urllib import urlencode,urlretrieve
from pickle import load,dump
from PIL import Image
from sys import platform
from os import write,system,remove,path,mkdir
from time import sleep,localtime, strftime


class pswd:
 usn=""
 pas=""
 
 def __init__(self):
    try:
     file = open(mypath+"chk.dat", "r")
     data = load(file)
     self.usn,self.pas = data
     file.close() 
    
    except:
     self.usn=raw_input("Enter Username: ")
     self.pas=getpass()
     data = [self.usn,self.pas]
     md()
     file = open(mypath+"chk.dat","w")
     dump(data, file)
     file.close()
 
 def give(self):
     return self.usn,self.pas


def clean(x=0):
	if x==0:
 	 if platform in ('win32', 'dos','ms-dos'):
          system('cls')
         else:
          erase()
	else:
	  system('clear')
        
def erase():
	sp=" "
	for i in range (0,110):
	  sp=sp+" "
	print('\033[1;1H '+sp+'\n'+sp+'\n'+sp+'\n'+sp+'\n'+'\033[1;23H ')

         
def login():
        clean()
        a=pswd()
        usn,pas=a.give()	
	link = "http://192.168.150.2:8080/clntAuth/clntAuth_main.jsp"
	url = {'loginID':usn,'loginpassword':pas,'method':'POST'}
	enc = urlencode(url)
	poster = Request(link, enc)
	try:
           response = urlopen(poster)
           log()
	except:
	   print "Response Error"



def timer():
	count =60
	print("\t\t\t\tTime Left Till Next Login Check:")
	while count>0:
  
	 m=count/60
	 s=count%60
	 if m<10:
	  m='0'+str(m)
	 if s<10:
	  s='0'+str(s)
 
	 write(1,"\r\t\t\t\t"+str(m)+':'+str(s))
	 sleep(1)
	 count=count-1
	clean()

def retIm():
	try:
          remove(mypath+"chk.jpg")
        except:
          clean()
	url = "http://a.wordpress.com/avatar/djays-32.jpg"
	try:
	 md()
         urlretrieve( url, mypath+"chk.jpg" )
        except:
         print "Plz Connect to Internet"
	try:
         im=Image.open(mypath+"chk.jpg")
         im.load()
         print "\t\t\t\tStatus: Connected"
	 return 0
 	except:
	 print "\t\t\t\tStatus: Re-Login"
	 return 20

def md():
 try:
  if path.exists(mypath)==0 :
   mkdir(mypath)
 except:
  print "Write Priviliges Required"

def log():
 date=strftime("%d-%b-%Y")
 time=strftime("%H:%M:%S", localtime())
 md()
 fout = open(mypath+date+".dat","a")
 fout.write(time+'\n')
 fout.close()




global p,mypath
p="/"
if platform in ('win32', 'dos','ms-dos'):
 p="\\"
mypath=path.abspath(path.dirname(__file__)) + p+"IALData"+p


i=1	
clean(1)
login()
while i:
        
	try:
         clean()
        
        except:
         print "Clean-Bug, Plz Report To Dj"
        print "\t\t\t\t******This Script has been made by Dj!! Visit djkaos.wordpress.com******"
	while retIm()==20:
	  login()
	  

	timer()
        



 



########################################## I-ON AUTO LOGIN SCRIPT v 3.0 #############################################################
##########################################        MADE BY DJAY          #############################################################
##########################################  http://djkaos.wordpress.com #############################################################

	



